package com.glsc.ngateway.opmanage.utils;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述: <br>
 *
 * @author gaojc
 * @date 2020/9/3 14:28
 */
public class JsonTool {
    private static Logger logger = LoggerFactory.getLogger(JsonTool.class);

    public static Long parseJsonId(String jsonStr) {
        String ret = parseJson(jsonStr, "id");
        return ret == null ? null : Long.valueOf(ret);
    }

    public static Long parseIdLong(String jsonStr, String id) {
        String ret = parseJson(jsonStr, id);
        return ret == null ? null : Long.valueOf(ret);
    }

    public static Integer parseIdInt(String jsonStr) {
        String ret = parseJson(jsonStr, "id");
        return ret == null ? null : Integer.valueOf(ret);
    }

    public static Integer parseIdInt(String jsonStr, String id) {
        String ret = parseJson(jsonStr, id);
        return ret == null ? null : Integer.valueOf(ret);
    }

    public static String parseJson(String jsonStr, String id) {
        JsonElement element = JsonParser.parseString(jsonStr);
        if (element == null) {
            return null;
        }
        JsonObject json = element.getAsJsonObject();
        if (json == null || json.get(id) == null || json.get(id).isJsonNull()) {
            return null;
        } else {
            return json.get(id).getAsString();
        }
    }

    public static List<Long> parseJsonIdsStr(String jsonStr) {
        List<Long> result = new ArrayList<>();
        JsonArray jsonArray = JsonParser.parseString(jsonStr).getAsJsonArray();
        if (jsonArray != null && jsonArray.size() > 0) {
            jsonArray.forEach(jsonElement -> {
                Long id = jsonElement.getAsJsonObject().get("id").getAsLong();
                if (id != null) {
                    result.add(id);
                }
            });
        }

        return result;
    }

    public static Integer parseJsonIdStr(String jsonStr) {
        JSONObject json = JSONObject.parseObject(jsonStr);
        if (null == json) {
            return -1;
        } else {
            Integer id = json.getInteger("id");
            if (null == id) {
                return -1;
            } else {
                return id;
            }
        }
    }


    /**
     * @param object
     * @return java.lang.String
     * @desc 对象转json
     */
    public static String objectToJson(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonList = null;
        try {
            jsonList = objectMapper.writeValueAsString(object);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            logger.error("Error", e);
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            logger.error("Error", e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            logger.error("Error", e);
        }
        return jsonList;
    }
}
